﻿using EA.BA;
using EA.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml;

namespace EAAdmin
{
    public abstract class WebPage : System.Web.UI.Page
    {
        protected static AuthorizePage AUTHORIZE_PAGE;
        private Dictionary<String, Object> attributes;
        protected Boolean isLoad=false;
        public Dictionary<String, Object> Attributes { 
            get
            {
                return attributes;
            }
            set
            {
                value = attributes;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ObjectBA.ConnectionString == null || ObjectBA.ConnectionString.Equals(""))
            {
                ObjectBA.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            }

            this.isLoad = false;
            attributes = new Dictionary<string, object>();
            if (IsAuthorize(Request, Response))
            {
                if (Request["action"] != null && !Request["action"].Equals(""))
                {
                    Type myTypeObj = this.GetType();
                    MethodInfo myMethodInfo = myTypeObj.GetMethod(Request["action"]);
                    object[] myParam = new object[] { Request, Response };
                    myMethodInfo.Invoke(this, myParam);

                    if (isLoad)
                    {
                        this.Load(sender, e);
                    }
                }
                else
                {
                    this.Load(sender, e);
                }

            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        protected void error(String content)
        {
            Attributes.Add("error", content);
        }

        protected void msg(String content)
        {
            Attributes.Add("msg", content);
        }

        protected abstract void Load(object sender, EventArgs e);

        public Boolean IsAuthorize(HttpRequest Request, HttpResponse Response)
        {
            if (AUTHORIZE_PAGE == null)
            {
                AUTHORIZE_PAGE = this.ParseAuthorizeXML();
            }
            if (AUTHORIZE_PAGE != null)
            {
                String roleID = "0";
                if (Session["ROLE_ID"] != null && !Session["ROLE_ID"].ToString().Equals(""))
                {
                    roleID = Session["ROLE_ID"].ToString();
                }
                Object[] list = AUTHORIZE_PAGE.Get(roleID);
                String pathCurrent = this.GetType().Name;
                if (list != null)
                {
                    Boolean isAllow = false;
                    for (int i = 0; !isAllow && i < list.Count(); i++)
                    {
                        if (list[i].Equals(pathCurrent))
                        {
                            isAllow = true;
                        }
                    }
                    return isAllow;
                }
            }
            return false;
        }

        //parse authorize.xml
        private AuthorizePage ParseAuthorizeXML()
        {
            AuthorizePage authPage = new AuthorizePage();
            String pathSource = Server.MapPath("~/authorize.xml");
            XmlDocument authorizeDom = new XmlDocument();
            authorizeDom.Load(pathSource);
            XmlNodeList authorizeList = authorizeDom.GetElementsByTagName("user");

            for (int i = 0; authorizeList != null && i < authorizeList.Count; i++)
            {
                String[] controllerArray = authorizeList[i].LastChild.InnerText.Split(',');
                for (int j = 0; controllerArray != null && j < controllerArray.Length; j++)
                {
                    String controller = controllerArray[j].Replace("\n", "");
                    authPage.Add(authorizeList[i].FirstChild.InnerText, controller.Trim());
                }
            }
            return authPage;
        }
    }
}